/**
 * Created by Administrator on 2017/6/30 0030.
 */
"use strict";

angular.module('app.sensor').controller('HistoricaldataCtrl', function ($scope,$stateParams, $http,$cookieStore) {
    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 50, 1000],
        select:'select',
        deviceid:'',
        timefrom:'',
        timeto: '',
        groupid:'',
        onChange: function () {
        }
    };
    $scope.send_num = 1;
    $scope.exportData = function () {
        var blob = new Blob(["\ufeff" + document.getElementById('exportable').innerHTML], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
        });
        saveAs(blob, "Report.xls");
    };
    //3.封装选择端口函数
    $scope.get_someport = function (port) {
        console.log(port);
        $scope.cur_port = port.port;
    }
    // 1,先获取端口号（哪种协议）封装函数
    $scope.get_port = function () {
        $http.get(baseurl + '/select_ports').then(function (res) {
            if (res.data.ret == 1) {
                $scope.ports = res.data.info;              
                //3.判断地址栏是否有参数 如果有 就用地址栏的参数 如果没有  就获取btn上的参数用
                if ($stateParams.port != '' || $stateParams.port != null) {
                    $scope.cur_port = $stateParams.port;                 
                } else {
                    $scope.cur_port = res.data.info[0].port + '';
                }
                $scope.get_all();
            }
           
        });
    }
    $scope.get_port();
   
    //2.封装获取字段/项目/设备分组函数
    $scope.get_all = function () {
        $http.get(baseurl + '/get_history/' + $scope.cur_port).then(function (res) {
            console.log(res.data);
        });
    }
   
    $http.get(baseurl + '/get_history/8053').then(function (res) {
        console.log(res.data);
    });










    function head_get(){
        $http.get(baseurl + '/table_columns/log_' + $scope.first_port).then(function (res) {
            //console.log(res.data);
            $scope.DATA = [];
            $scope.DATA_name = [];
              //console.log(res.data.info);
            if (res.data.ret == 1) {
                for(var i = 0 ; i < res.data.info.length; i++){
                    if(res.data.info[i].COLUMN_NAME === 'id'){
                        res.data.info[i].column_comment = 'id'
                    }else if(res.data.info[i].COLUMN_NAME === 'deviceid'){
                        res.data.info[i].column_comment = '设备编号'
                    }else if(res.data.info[i].COLUMN_NAME === 'fid'){
                        res.data.info[i].column_comment = '公司'
                    }else if(res.data.info[i].COLUMN_NAME === 'projectid'){
                        res.data.info[i].column_comment = '项目'
                    } else if (res.data.info[i].COLUMN_NAME === 'groupid') {
                        res.data.info[i].column_comment = '组'
                    }
                    if (res.data.info[i].column_comment !== 'id' && res.data.info[i].column_comment !== '公司'&& res.data.info[i].column_comment !== '组' && res.data.info[i].column_comment !== '项目') {
                        $scope.DATA_name.push(res.data.info[i].column_comment);
                        $scope.DATA.push(res.data.info[i].COLUMN_NAME)
                    }
                }
                $scope.get_list();
            }
        });
    }
    $scope.get_list = function(){
        var page_obj = {
            page:$scope.paginationConf.currentPage,
            rows:$scope.paginationConf.itemsPerPage
        };
        var pid = $scope.paginationConf.projectid;
        var did = $scope.paginationConf.deviceid;
        var gid = $scope.paginationConf.groupid;
        var tid = $scope.paginationConf.timefrom;
        var toid = $scope.paginationConf.timeto;
        var query = '?projectid==' + pid + '&groupid==' + gid + '&time=' + tid + ',' + toid;
        if ($scope.first_port != null && $scope.first_port != '') {
            var table = 'view_log_' + $scope.first_port + '_group1';
            $http.post(baseurl+'/list/select/'+table+query,page_obj).then(function(res){
                if (res.data.ret === 1 && res.data.rows !== null) {
                    var arr = res.data.rows;
                    for (var i = 0; i < arr.length; i++) {
                        var obj = arr[i];
                        var time = obj.time;
                        obj.time = moment(time).format("YYYY-MM-DD HH:mm:ss");
                        if (obj.time == 'Invalid date') {
                            obj.time = time;
                        }
                    }
                }
                $scope.datas = arr;
                $scope.paginationConf.totalItems = res.data.records;
            });
            if($scope.send_num === 1){
                $http.get(baseurl + '/select_deviceid?port==' + $scope.first_port).then(function (res) {
                    //console.log(res.data);
                    $scope.send_num = 0;
                    if(res.data.ret === 1 ) {
                        $scope.devices = res.data.info;
                        if(res.data.info.length > 0){
                           // $scope.paginationConf.deviceid = res.data.info[0].id;
                        }
                    }
                });
            }
        }else{
            $scope.datas = [];
            $scope.paginationConf.totalItems = 0;
        }
    };
    if ($stateParams.deviceid !== null && $stateParams.deviceid !== '') {
        $scope.paginationConf.deviceid = $stateParams.deviceid;
        $scope.get_list();
    }
    $scope.send = function () {
        $scope.paginationConf.select = 'search';
        if ($('#startdate').val())
            $scope.paginationConf.timefrom = $('#startdate').val();
        if ($('#finishdate').val())
            $scope.paginationConf.timeto = $('#finishdate').val();
        //head_get();
    };
    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.get_list);
});